Return lots of stars


Posted by Christy on 2022-04-19

Description: Write a function named makeStars that accepts a number n and return the star like below.

Note: Don’t use repeat()

makeStars(1) // *

makeStars(2) 

// *
// **

makeStars(5) 

// *
// **
// ***
// ****
// *****

The answers:

function makeStars(n) {
  let star = "";
  for (let i = 1; i <= n; i++) {
    console.log((star += "*"));
  }
  return star;
}

makeStars(5);
function makeStars(n) {
  for (let i = 1; i <= n; i++) {
    let star = "";
    for (let j = 1; j <= i; j++) {
      star += "*";
    }
    console.log(star);
  }
  return makeStars;
}

makeStars(3);









Related Posts

Chapter 1. 重構:第一個範例

Chapter 1. 重構:第一個範例

8 月開始上班,切換行銷與寫程式的腦袋

8 月開始上班,切換行銷與寫程式的腦袋

3. java 17  範例目錄

3. java 17 範例目錄


Comments